/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service.promo.impl;

import dao.promo.PromoDisitemDao;
import java.util.List;
import java.util.Map;
import model.promo.PromoDisitem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.promo.PromoDisitemService;
import utility.NameValueList;
import utility.StringUtils;

/**
 *
 * @author Lalugina
 */
@Service("promoDisitemService")
@Transactional(readOnly=true)
public class PromoDisitemServiceImpl implements PromoDisitemService{
    
    String mainQuery = "select promoDisitem from PromoDisitem promoDisitem "+
               "left join fetch promoDisitem.promo promo left join fetch promoDisitem.item item ";
    
    @Autowired private PromoDisitemDao dataDao;

    @Override
    @Transactional(readOnly=false)
    public boolean save(PromoDisitem data) {
        return dataDao.save(data);
    }

    @Override
    @Transactional(readOnly=false)
    public boolean delete(PromoDisitem data) {
        return dataDao.delete(data);
    }

    @Override
    public PromoDisitem get(long id) {
        String query = mainQuery+" where promoDisitem.id = "+id;
        return dataDao.getByCustomQuery(query);
    }

    @Override
    public Long count(Map<NameValueList, Object> params) {
        String query= "select count(*) from PromoDisitem promoDisitem "+StringUtils.getFilter(params);
        return dataDao.count(query);
    }

    @Override
    public List<PromoDisitem> getAll(int promoId) {
        String query = mainQuery+" where promoDisitem.promo.id = "+promoId+" order by item.nama";
        return dataDao.getAll(query);
    }
    
}
